// RadioButton.cpp
//
// A button which stays pressed or unpressed after a click,
// and only one instance of the class can be pressed at a time.
//
// Nick Kitten
// 2009

#include "RadioButton.h"


RadioButton::RadioButton( char * spriteFile, int ID, int x, int y, void (*leftCallback)(), 
		int width, int height )
		: Button( spriteFile, ID, "", x, y, leftCallback, 0, width, height )
{
}

RadioButton::~RadioButton()
{
}

// override base class display()
void RadioButton::display()
{
	if( ! getVisible() )
		return;

	Position mousePos = Position( dbMouseX(), dbMouseY() );

	lastPressed = buttonPressed;
	buttonPressed = dbMouseClick();

	if( inRect( mousePos.x, mousePos.y ) )
		if( ! buttonPressed && ! isPressed() )
		{
			dbSetSpriteFrame( getID(), 2 );

			// button clicked and released
			if( lastPressed == 1 )
			{
				leftClickFunc();
				pressedID = getID();
			}
			
		}
		else
			dbSetSpriteFrame( getID(), 3 );
	else if( isPressed() )
		dbSetSpriteFrame( getID(), 3 );
	else
		dbSetSpriteFrame( getID(), 1 );

	dbPasteSprite( getID(), hitBox.minPoint.x, hitBox.minPoint.y );
}

//
bool RadioButton::isPressed() const
{
	return pressedID == getID();
}

// the button which is currently pressed
int RadioButton::pressedID = 1;